<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\App;

class SummaryUser extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //关联模型
    protected array $hasOne = ['user'];

    //主模型
    protected string $model = '\app\common\model\SummaryUser';

    public function index(): string
    {
        $tableFront = Builder::Table()
            ->addColumn(['field' => 'code', 'title' => '编号', 'fixed' => 'left', 'templet' => '<div>{{d.user.code}}</div>', 'totalRowText' => '合计'])
            ->addColumn(['field' => 'nickname', 'title' => '昵称', 'templet' => '<div>{{d.user.nickname}}</div>'])
            ->addColumn(['field' => 'mobile', 'title' => '手机号', 'templet' => '<div>{{d.user.mobile}}</div>']);
        foreach (getBonusTitle(2) as $k => $v) {
            $tableFront->addColumn(['field' => $k, 'templet' => '<span>{{d.' . $k . '}}</span>', 'title' => $v, 'totalRow' => true]);
        }
        $tableFront->addColumn(['field' => 'update_time', 'title' => '更新时间']);
        $tableFront->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200]);

        return $tableFront->addSearch([
            ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id']
        ], false)
            ->addTopButtons(['export'])
            ->addRightButtons([
                'summary_info' => [
                    'title' => '查看明细',
                    'type'  => 'summary_info',
                    'url'   => url('summary_info')->build(),
                    'event' => '_openNewPage',
                    'size'  => '90%',
                    'site'  => 'r'
                ]
            ])
            ->setTotalRaw('true')
            ->addJsUrl(['summary'])
            ->fetch();
    }


    public function summary_info($id)
    {
        $info = \app\common\model\SummaryUser::find($id);
        $url = url('account/bonus') . '?uid=' . $info->user->code . '&summary=1';
        return redirect($url);

    }

}
